var keyPrefix = 'FlickrInlineEditLight.';

GM_log = function(message) {
	console.info('FIEL:' + message);
}

GM_addStyle = function(css) {
        var head = document.getElementsByTagName('head')[0], style = document.createElement('style');
        if (!head) {return}
        style.type = 'text/css';
        try {style.innerHTML = css} catch(x) {style.innerText = css}
        head.appendChild(style);
}

GM_getValue = function(key, defValue) {
	var retval = window.localStorage.getItem(keyPrefix + key);
	if (!$chk(retval)) {
		return defValue;
	}
	return retval;
}

GM_setValue = function(key, value) {
	try {
		window.localStorage.setItem(keyPrefix + key, value);
	} catch(e) {
		GM_log("error setting value: " + e);
	}
}

GM_deleteValue = function(key) {
	try {
		window.localStorage.removeItem(keyPrefix + key);
	} catch(e) {
		GM_log("error removing value: " + e);
	}
}

GM_listKeys = function(regexp, callback) {
	var list = [];
	var reKey = new RegExp("^" + keyPrefix); // first, make sure we only use our own keys
	for (var i = 0, il = window.localStorage.length; i < il; i++) {
		// only use the script's own keys
		var key = window.localStorage.key(i);
		try {
			if (key.test(reKey)) {
				var unprefixedKey = key.replace(keyPrefix, '');
				if (regexp) {
					if (unprefixedKey.test(regexp)) {
						if (callback) {
							callback(unprefixedKey);
						} else {
							list.push(unprefixedKey);
						}
					}
				} else {
					if (callback) {
						callback(unprefixedKey);
					} else {
						list.push(unprefixedKey);
					}
				}
			}
		} catch(e) {
			// ignore
		}
	}
	return list;
}

GM_getObject = function(key) {
	var value = GM_getValue(key);
	if ($chk(value)) {
		try {
			return {
				value: JSON.parse(value),
				key_present: true
			};

		} catch(e) {
			var error = "error parsing (" + e + ") object '" + key + "': " + value;
			GM_log(error);
			return {
				value: null,
				key_present: true,
				error: error
			};
		}
	}
	return {
		value: null,
		key_present: false
	};
}

GM_storeObject = function(key, value) {
	GM_setValue(key, JSON.stringify(value));
}

var getJSVariable = function(regex) {
	// Thanks to Vispillo for this compact code
	var retval;
	$$('script').each(function(script) {
		if (retval != undefined) {
			return;
		}
		var html = script.innerHTML;
		try {
			retval = html.match(regex)[1];
		} catch(e) {
		}
	});
	return retval;
}

GM_getMagisterLudi = function() {
	return getJSVariable(/[\"\']api_key[\"\'][ :]+[\'\"]([^\'\"]+)[\'\"]/);
}

GM_getPhotostreamOwnerNsid = function() {
	var reMatch = /photostream_owner_nsid[ =]+\"([^\"]+)\"/;
	var retval = getJSVariable(reMatch);
	if (!$chk(retval)) {
		reMatch = /\"owner_nsid\"[ :]+\"([^\"]+)\"/;
		retval = getJSVariable(reMatch);
	}
	return retval;
}

GM_getUserNsid = function() {
	var reMatch = /global_nsid[ =]+\'([^\']+)\'/;
	var retval = getJSVariable(reMatch);
	if (!$chk(retval)) {
		reMatch = /\"nsid\"[ :]+\"([^\"]+)\"/;
		retval = getJSVariable(reMatch);
	}
	return retval;
}

GM_getGroupId = function() {
	var reMatch = /\"id\"[ :]+[\'\"]([^\'\"]+@\w\d{2})[\'\"]/;
	var groupId = getJSVariable(reMatch);
	if (!$chk(groupId)) {
		var rssFeeds = $$('a[href*=groups_discuss.gne]');
		if (rssFeeds.length > 0) {
			var groupFeed = rssFeeds[0];
			try {
				groupId = groupFeed.get('href').match(/(\d+@\w\d{2})/)[1];
			} catch(e) {
				// ignore
			}
		}
	}
	return groupId;
}

GM_getAuthHash = function() {
	return getJSVariable(/[\"\']auth_hash[\"\'][ :]+[\'\"]([^\'\"]+)[\'\"]/);
}

GM_getAuthToken = function() {
	return getJSVariable(/[\"\']auth_token[\"\'][ :]+[\'\"]([^\'\"]+)[\'\"]/);
}

